package TreeStructure;

import java.util.ArrayList;
import java.util.List;

public class TreeNode<T> {
	private T data;
	private TreeNode<T> parent;
	private List<TreeNode<T>> children;
	private int depth;

	// default constructor
	public TreeNode() {
	}

	// constructor with 1 parameter
	public TreeNode(T data) {
		this.data = data;
	}

	//returns the level 1st parent just under Root.
	public TreeNode<T> getFirstLevelParent() {
		TreeNode<T> node = this;
		while (node.getParent().getParent() != null) {
			node = node.getParent();

		}
		return node;
	}

	public int getDepth() {
		TreeNode<T> node = this;
		while (node.getParent() != null) {
			node = node.getParent();
			depth++;
		}
		return depth;
	}

	public void setDepth(int depth) {
		this.depth = depth;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public TreeNode<T> getParent() {
		return parent;
	}

	public void setParent(TreeNode<T> parent) {
		this.parent = parent;
	}

	public List<TreeNode<T>> getChildren() {
		if (children == null) {
			children = new ArrayList<TreeNode<T>>();
		}
		return children;
	}

	public void setChildren(List<TreeNode<T>> children) {
		this.children = children;
	}

}
